package controller;

import bean.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import service.SystemService;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/")
public class SystemController {

    @Resource
    private SystemService systemService;

    @RequestMapping("index")
    public String index() {
        return "index";
    }

    @RequestMapping("dashboard")
    public String dashboard() {
        return "dashboard";
    }

    @RequestMapping("loginSubmit")
    public String loginSubmit(String username, String password, HttpSession session) {
        User user = systemService.authenticate(username);
        if (user != null && user.getPassword().equals(password)) {
            session.setAttribute("loginUser", user);
            return "redirect:dashboard";
        }
        return "redirect:index";
    }

    @RequestMapping("logout")
    public String logout(HttpSession session) {
        session.removeAttribute("loginUser");
        session.invalidate();
        return "redirect:index";
    }
}
